package cn.com.callback;

import cn.com.elite.EliteBean
import cn.com.kh.KHUtil
import net.sf.json.JSONArray;
import net.sf.json.JSONObject
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author ThinkPad*
 */

public class getRight implements ServiceBackInvoke {

    private static final Log log = LogFactory.getLog(getRight.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        log.debug("获取广告区域信息...");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        JSONObject result = new JSONObject();

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);
        String inData = "";
        if (getMethod == "GET") {
            inData = request.getParameterNames().toString();
            log.debug("原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("原始Post请求参数:" + inData);
        }

        try {
            // 解析参数信息
            String client_guid = "";
            String queue = "";

            try {
                client_guid = inData.split("&")[0].split("=")[1];
                queue = inData.split("&")[1].split("=")[1];
            } catch (Exception e) {
                log.warn("解析参数信息 异常", e);
            }
            queue = KHUtil.isNull(queue) ? "-1" : queue;
            log.debug("原始请求参数client_guid:" + client_guid + " queue:" + queue);

            String rightDataSQL = "Select Orderid, Questionname, Content From Chat_Rightdata_Question Where Type = '1' And Queueid = " + queue + " Order By Orderid Limit 6";
            List rightDataRes = eliteBean.chaxun_sql(rightDataSQL, "dataSource");
            log.debug("通过队列查询广告数据结果:" + rightDataRes.toString());

            JSONArray rightDataArr = JSONArray.fromObject(rightDataRes);

            // 如果通过队列查询结果为空则查询默认值
            if (rightDataArr.size() <= 0) {
                rightDataSQL = "Select Orderid, Questionname, Content From Chat_Rightdata_Question Where Type = '1' And Queueid Is Null Order By Orderid Limit 6";
                rightDataRes = eliteBean.chaxun_sql(rightDataSQL, "dataSource");
                log.debug("查询默认广告数据结果:" + rightDataRes.toString());

                rightDataArr = JSONArray.fromObject(rightDataRes);
            }

            result.put("status", "success");
            result.put("data", rightDataArr);

            // 新版本Chat.react右侧广告配置
            JSONObject rightData = new JSONObject();

            JSONArray arrayArr = new JSONArray();
            if (rightDataArr.size() > 0) {
                JSONObject arrayObj = new JSONObject();
                arrayObj.put("type", "richText");
                arrayObj.put("title", "");
                arrayObj.put("text", rightDataArr.getJSONObject(0).getString("Content"));
                arrayArr.add(arrayObj);
            }
            rightData.put("array",arrayArr);

            JSONObject justifyContentObj = new JSONObject();
            justifyContentObj.put("justifyContent", "flex-end");

            rightData.put("setting", justifyContentObj);

            result.put("right", rightData);

            log.debug("获取广告区域信息：" + result.toString());
        } catch (Exception e) {
            log.error("接口处理异常", e);

        }
        return result;
    }
}